var e = require;
var t = module;
var o = exports;
var n;
Object.defineProperty(o, "__esModule", {value: !0}), (o.NativeType = void 0);
var r,
    s = e("SDK"),
    c = e("VivoAd"),
    l = e("AdUtils"),
    d = cc._decorator,
    u = d.ccclass,
    p = d.property;
(function (e) {
    (e[(e.INSET = 0)] = "INSET"),
        (e[(e.BANNER = 1)] = "BANNER"),
        (e[(e.ICON = 2)] = "ICON"),
        (e[(e.SPLASH = 3)] = "SPLASH"),
        (e[(e.BANNER_BUTTON = 4)] = "BANNER_BUTTON"),
        (e[(e.BANNER_NO_NATIVE = 5)] = "BANNER_NO_NATIVE"),
        (e[(e.BANNER_LITTLE_BUTTON = 6)] = "BANNER_LITTLE_BUTTON"),
        (e[(e.ICON_GAME = 7)] = "ICON_GAME"),
        (e[(e.INSET_NO_NATIVE = 8)] = "INSET_NO_NATIVE");
})((r = o.NativeType || (o.NativeType = {})));
var h = (function (e) {
    function t() {
        var t = (null !== e && e.apply(this, arguments)) || this;
        return (
            (t.iconShow = !1),
            (t._closeButton = null),
            (t._content = null),
            (t._type = r.INSET),
            (t._nativeCurrentAd = null),
            (t._config = null),
            (t._adCallBack = null),
            (t._clickCallBack = null),
            (t._showCallBack = null),
            (t._closeCallBack = null),
            (t._title = null),
            (t._info = null),
            (t._isClick = !1),
            (t._splashCallBacks = null),
            (t._labelCall = null),
            (t._vivoNativeDate = null),
            t
        );
    }
    return (
        __extends(t, e),
        (t.prototype.start = function () {
            if ((l.default.instance.log("-----------------iconShow: " + this.iconShow), this.iconShow)) {
                var e = c.default.Instance.getVivoNativeData();
                if (null == e || !e.isReady()) return void (this.node.active = !1);
                this.initData(e),
                    this.listionCall(
                        function () {
                            s.default.Instance.reportLog(s.NodeAction.NATIVE_SHOW, "原生icon展示");
                        },
                        function () {
                            c.default.Instance.creatorNativeData(),
                                s.default.Instance.reportLog(s.NodeAction.NATIVE_CLICK, "原生icon点击");
                        },
                        function () {
                            s.default.Instance.reportLog(s.NodeAction.NATIVE_CLOSE, "原生icon关闭");
                        }
                    ),
                    this.showAd(r.ICON_GAME, c.default.Instance.getConfig()),
                    (this.node.active = !0);
            }
        }),
        (t.prototype.initButton = function (e) {
            if (e) {
                this._content = e.getChildByName("contentAd").getComponent(cc.Sprite);
                var t = e.getChildByName("Button");
                if (
                    t &&
                    t.active &&
                    (t.on(cc.Node.EventType.TOUCH_START, this.adClick, this),
                    this._type == r.BANNER_BUTTON || this._type == r.BANNER_LITTLE_BUTTON)
                ) {
                    if (((t.active = !1), this._config.inser_ad_node)) {
                        var o = JSON.parse(this._config.inser_ad_node)[0];
                        o && o.bntDetails && o.bntDetails > 0 && (t.active = !0);
                    }
                    t.runAction(cc.repeatForever(cc.sequence(cc.scaleTo(0.5, 1.1), cc.scaleTo(0.5, 1))));
                }
            }
        }),
        (t.prototype.onDisable = function () {
            this._adCallBack && this.unschedule(this._adCallBack);
        }),
        (t.prototype.initData = function (e) {
            this._vivoNativeDate = e;
        }),
        (t.prototype.init = function () {
            var e = this;
            this.node.active = !0;
            var t = this;
            if (((this._nativeCurrentAd = this._vivoNativeDate.getNativeDate()), null == this._nativeCurrentAd))
                return (
                    console.log("------没有广告数据"),
                    void c.default.Instance.creatorNativeData(function (o) {
                        o && ((e._vivoNativeDate = c.default.Instance.getVivoNativeData()), t.init());
                    })
                );
            var o = this.node.getChildByName("content");
            if (o) {
                var n = o.getChildByName("adId");
                if (
                    (n &&
                        ((n.active = !1),
                        (n.y = -50),
                        (n.x = -10),
                        (n.color = new cc.Color(255, 0, 0)),
                        (n.getComponent(cc.Label).string = this._nativeCurrentAd.adId.toString())),
                    this._type == r.SPLASH)
                ) {
                    var i = o.getChildByName("closeContinu");
                    i.on(cc.Node.EventType.TOUCH_START, this.close, this);
                    var a = i.getChildByName("cLabel").getComponent(cc.Label);
                    if ((l.default.instance.log("----------splash label: " + a), a)) {
                        var s = 3;
                        (t._labelCall = function () {
                            --s <= 0 && (l.default.instance.log("----------开屏关闭: " + a), t.hide()),
                                (a.string = s + "秒 跳过"),
                                l.default.instance.log("----------splash label: " + a.string);
                        }),
                            t.schedule(t._labelCall, 3);
                    }
                }
            }
            var d = this._nativeCurrentAd.imgUrlList[0];
            if (this._type != r.ICON && this._type != r.BANNER_BUTTON && this._type != r.ICON_GAME && o) {
                var u = o.getChildByName("title");
                u && u.active && ((t._title = u.getComponent(cc.Label)), (t._title.string = t._nativeCurrentAd.title));
                var p = o.getChildByName("info");
                if (
                    (p &&
                        p.active &&
                        ((t._info = p.getComponent(cc.Label)), (t._info.string = t._nativeCurrentAd.desc)),
                    (d = this._nativeCurrentAd.icon),
                    this._type != r.BANNER && this._type != r.BANNER_LITTLE_BUTTON)
                ) {
                    var h = o.getChildByName("icon");
                    if (h && h.active) {
                        var f = h.getComponent(cc.Sprite);
                        this.getImageSpriteFrame(d)
                            .then(function (e) {
                                f.spriteFrame = e;
                            })
                            .catch(function (e) {
                                console.log(e),
                                    t.getImageSpriteFrame(d).then(function (e) {
                                        f.spriteFrame = e;
                                    });
                            });
                    }
                }
            }
            (d = this._type == r.ICON ? this._nativeCurrentAd.icon : this._nativeCurrentAd.imgUrlList[0]),
                this.getImageSpriteFrame(d)
                    .then(function (e) {
                        t._content.spriteFrame = e;
                    })
                    .catch(function (o) {
                        console.log(o),
                            (d = e._nativeCurrentAd.imgUrlList[0]),
                            t
                                .getImageSpriteFrame(d)
                                .then(function (e) {
                                    t._content.spriteFrame = e;
                                })
                                .catch(function (e) {
                                    console.log(e),
                                        t.getImageSpriteFrame(d).then(function (e) {
                                            t._content.spriteFrame = e;
                                        });
                                });
                    });
        }),
        (t.prototype.listionCall = function (e, t, o) {
            (this._showCallBack = e), (this._clickCallBack = t), (this._closeCallBack = o);
        }),
        (t.prototype.closeSize = function () {
            if (null != this._closeButton && this._config && this._closeButton) {
                var e = JSON.parse(this._config.inser_ad_node);
                if (e) {
                    var t = e[0];
                    if (t) {
                        var o = t.size;
                        if (o && o.enable) {
                            var n, i;
                            (n = null == o.width || 0 == o.width ? 35 : o.width < 20 ? 20 : o.width),
                                (i = null == o.height || 0 == o.height ? 35 : o.height < 20 ? 20 : o.height),
                                (this._closeButton.width = n),
                                (this._closeButton.height = i);
                        }
                    }
                }
            }
        }),
        (t.prototype.showAd = function (e, t, o, n, i) {
            var a = this;
            (this._type = e), (this._config = t);
            var s = this.node.getChildByName("content");
            if ((this.initButton(s), s)) {
                (this._closeButton = s.getChildByName("close")),
                    this.closeSize(),
                    s.on(cc.Node.EventType.TOUCH_START, this.adClick, this),
                    this._closeButton.on(cc.Node.EventType.TOUCH_START, this.close, this),
                    (this.node.active = !1),
                    (this._splashCallBacks = i),
                    this.show();
                var d = this._config.flush_ad_time,
                    u = this;
                if (
                    e == r.BANNER ||
                    e == r.ICON ||
                    e == r.BANNER_BUTTON ||
                    e == r.ICON_GAME ||
                    e == r.BANNER_LITTLE_BUTTON
                ) {
                    if (((this.node.x = o), (this.node.y = n), d <= 0))
                        return void l.default.instance.log("-----------不刷新广告----------");
                    (this._adCallBack = function () {
                        l.default.instance.log("-----------广告刷新----------");
                        var e = a.node.getChildByName("content");
                        e && (e.active = !0), (u._vivoNativeDate = c.default.Instance.getVivoNativeData()), u.show();
                    }),
                        this.schedule(this._adCallBack, d);
                }
            }
        }),
        (t.prototype.show = function () {
            (this._isClick = !1),
                this._vivoNativeDate &&
                    (this.init(), this._vivoNativeDate.reportAdShow(), this._showCallBack && this._showCallBack());
        }),
        (t.prototype.onDestroy = function () {
            l.default.instance.log("-------广告 destroy " + this._type);
        }),
        (t.prototype.hide = function (e) {
            if ((void 0 === e && (e = !0), this.node))
                if (e) this.unscheduleAllCallbacks(), this.node.removeFromParent();
                else if (this._type != r.ICON_GAME) {
                    var t = this.node.getChildByName("content");
                    t && (t.active = !1);
                } else this.node.active = !1;
            this._closeCallBack && this._closeCallBack(),
                this._type == r.SPLASH &&
                    (this._splashCallBacks && this._splashCallBacks(), this.unschedule(this._labelCall));
        }),
        (t.prototype.adClick = function () {
            l.default.instance.log("-----------adClick 点击广告----------"),
                this._isClick ||
                    ((this._isClick = !0),
                    this._vivoNativeDate &&
                        (this._vivoNativeDate.reportAdClick(), this._clickCallBack && this._clickCallBack()),
                    this._type == r.INSET || this._type == r.SPLASH
                        ? this.hide(!0)
                        : ((this._vivoNativeDate = c.default.Instance.getVivoNativeData()),
                          null != this._vivoNativeDate && this._vivoNativeDate.isReady()
                              ? this.show()
                              : this.hide(!1)));
        }),
        (t.prototype.close = function () {
            l.default.instance.log("-----------close 关闭广告----------"),
                (this._isClick = !0),
                this._type == r.INSET || this._type == r.SPLASH ? this.hide() : this.hide(!1);
        }),
        (t.prototype.getImageSpriteFrame = function (e) {
            return new Promise(function (t, o) {
                console.log("图片下载地址：", e),
                    null == e ||
                        e.length <= 0 ||
                        cc.loader.load({url: e, type: "jpg"}, function (n, i) {
                            if (n) return console.log("图片下载失败", e, n), void o("图片下载失败: " + e);
                            var a = new cc.SpriteFrame(i);
                            t(a);
                        });
            });
        }),
        __decorate([p(cc.Boolean)], t.prototype, "iconShow", void 0),
        __decorate([u], t)
    );
})(cc.Component);
o.default = h;
